package com.hand.lwf24378.materials.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.github.pagehelper.PageInfo;
import com.hand.lwf24378.materials.domain.Materials;
import com.hand.lwf24378.materials.domain.Result;
import com.hand.lwf24378.materials.dto.SelectOrderHelper;
import com.hand.lwf24378.materials.service.MaterialsService;
import com.hand.lwf24378.materials.utils.ResultUtil;
import com.hand.lwf24378.materials.utils.WebFilenameUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author : WaveLee
 * @date : 2019/7/19 15:41
 * @description :
 */
@CrossOrigin
@RestController
@Api(tags = "物流管理接口")
public class MaterialsController {
    @Autowired
    MaterialsService materialsService;

    @PostMapping(value = "/add")
    @ApiOperation(value = "新增")
    public Result<Materials> add(@Valid Materials materials, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        return ResultUtil.success(materialsService.add(materials));
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "更新")
    public Result<Materials> update(@Valid Materials materials, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        return ResultUtil.success(materialsService.update(materials));
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation(value = "删除")
    public Result<Object> delete(@RequestParam Long itemId){
        materialsService.delete(itemId);
        return ResultUtil.success();
    }

    @DeleteMapping(value = "/deleteInBatch")
    @ApiOperation(value = "通过主键数组itemIds[]批量删除")
    public Result<Object> deleteInBatch(@RequestParam Long[] itemIds){
        materialsService.deleteInBatch(itemIds);
        return ResultUtil.success();
    }

    @GetMapping(value = "/getById")
    @ApiOperation(value = "通过itemId查询单个")
    public Result<Materials> getById(@RequestParam Long itemId){
        return ResultUtil.success(materialsService.getById(itemId));
    }

    @GetMapping(value = "/getAll")
    @ApiOperation(value = "查询所有")
    public Result<List<Materials>> getAll(){
        return ResultUtil.success(materialsService.getAll());
    }

    @GetMapping(value = "/selectItem")
    @ApiOperation(value = "通过条件查询并排序")
    public Result<PageInfo<Materials>> selectItem(SelectOrderHelper selectOrderHelper){
        return ResultUtil.success(new PageInfo<>(materialsService.selectItem(selectOrderHelper)));
    }

    @GetMapping(value = "/getByDateToExcel")
    @ApiOperation(value = "通过条件查询导出")
    public Result getByDateToExcel(
            SelectOrderHelper selectOrderHelper,
            HttpServletResponse response) throws IOException {
        selectOrderHelper.setRows(999999999);
        List<Materials> materials = materialsService.selectItem(selectOrderHelper);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("物料清单","sheet1" ),
                Materials.class,materials);

        Date time = new Date();
        String fileName = "物料清单" + time.getTime() + ".xls";
        response.setCharacterEncoding("UTF-8");
        response.addHeader("Content-Disposition", WebFilenameUtils.disposition(fileName));

        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();

        return ResultUtil.success();
    }
}
